var fs = require('fs');
const path = require('path')
let pkg = {};

try {
  pkg = require('./package.json');
} catch (e) {
  console.info('_____version update to error: ');
  console.info(e);
}
try {
  const gitHeadInfo = fs.readFileSync(path.join(__dirname, '.git', 'HEAD'), 'utf8');
  const match = /ref: refs\/heads\/([^\n]+)/.exec(gitHeadInfo);

  if (match) {
    const version = match[1].split('/')[1];
    if (version) {
      console.info('_____version update to: ' + version);
      pkg.version = version;
      fs.writeFileSync('./package.json', JSON.stringify(pkg, null, '  ') + '\n', 'utf8');
    }
  }
} catch (e) {
  console.error(e);
}